<template>
    <div class="login">
        <div class="login_adv">
            <div class="login_adv__title">
                <h2>GouUI</h2>
                <h4>Vue Admin Aigc</h4>
                <p>基于Vue3 + ElementPlus 的中后台前端解决方案。</p>
            </div>
            <div class="login_adv__mask"></div>
            <div class="login_adv__bottom">
                Copyright © 2024-2025 GouUI All Rights Reserved.
            </div>
        </div>
        <div class="login_main">
            <div class="login-form">
                <div class="login-top">
                    <h3 class="title">Vue Admin Aigc</h3>
                    <img class="login-header" src="@/assets/images/login_header.jpg" />
                </div>
                <div class="login-wrap">
                    <el-form ref="loginRef" :model="loginForm" :rules="loginRules">
                        <el-form-item prop="username">
                            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
                                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
                                @keyup.enter.native="handleLogin">
                                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                            </el-input>
                        </el-form-item>
                        <el-form-item style="width: 100%">
                            <el-button :loading="loading" size="large" type="primary" style="width: 100%"
                                @click.native.prevent="handleLogin" round>
                                <span v-if="!loading">登 录</span>
                                <span v-else>登 录 中...</span>
                            </el-button>
                            <div style="float: right" v-if="register">
                                <router-link class="link-type" :to="'/register'">立即注册</router-link>
                            </div>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
        </div>
        <!--  底部  -->
        <div class="el-login-footer">
            <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
        </div>
    </div>



</template>

<script setup>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { useUserStore } from '@/store/modules/user'
const codeUrl = ref("");
const router = useRouter();
const route = useRoute();
const { proxy } = getCurrentInstance();
const userStore = useUserStore();
const redirect = ref(undefined)
const loginForm = ref({
    username: "admin",
    password: "admin123"
})
const loading = ref(false)
watch(route, newRoute => {
    // console.log(newRoute)
    redirect.value = newRoute.query && newRoute.query.redirect
}, { immediate: true })


//校验
const loginRules = {
    username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
    password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
}
//登录提交数据
function handleLogin() {
    proxy.$refs.loginRef.validate((valid) => {
        if (valid) {
            loading.value = true
            userStore.login(loginForm.value).then(res => {
                router.push(redirect.value || '/')
            }).catch((err) => {
                // console.log(err)
            })
        } else {
            // console.log("校验失败")
        }
    })
}

function getCookie() {
    const username = Cookies.get("username");
    const password = Cookies.get("password");
    loginForm.value = {
        username: username === undefined ? loginForm.value.username : username,
        password: password === undefined ? loginForm.value.password : password
    };
}


</script>

<style lang="scss" scoped>
.login {
    width: 100%;
    height: 100%;
    background: #fff;
    display: flex;

    .login_adv {
        width: 33.33333%;
        background-color: #555;
        background-size: cover;
        background-position: center center;
        background-repeat: no-repeat;
        position: relative;
        background-image: url("../assets/images/auth_banner.jpg");

        .login_adv__title {
            color: #fff;
            padding: 40px;
            position: absolute;
            top: 0px;
            left: 0px;
            right: 0px;
            z-index: 2;

            h2 {
                font-size: 40px;
            }

            h4 {
                font-size: 18px;
                margin-top: 10px;
                font-weight: normal;
            }

            p {
                font-size: 14px;
                margin-top: 10px;
                line-height: 1.8;
                color: rgba(255, 255, 255, 0.6);
            }
        }

        .login_adv__mask {
            position: absolute;
            top: 0px;
            left: 0px;
            right: 0px;
            bottom: 0px;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1;
        }

        .login_adv__bottom {
            position: absolute;
            left: 0px;
            right: 0px;
            bottom: 0px;
            color: #fff;
            padding: 40px;
            background-image: linear-gradient(transparent, #000);
            z-index: 3;
            font-size: 12px;
        }
    }

    .login_main {
        flex: 1;
        overflow: auto;
        display: flex;

        .login-form {
            width: 480px;
            margin: auto;
            background: rgb(255, 255, 255);
            border-radius: 12px;
            box-shadow: rgba(56, 56, 138, 0.05) 0px 3.25px 4px -1.5px,
                rgba(56, 56, 138, 0.15) 0px 24.5px 70px -11.25px;
            overflow: hidden;

            .login-top {
                position: relative;
                overflow: hidden;

                .title {
                    margin: 0;
                    text-align: center;
                    color: #ffffff;
                    position: absolute;
                    z-index: 2;
                    left: 32px;
                    top: 40px;
                    font-weight: 600;
                }

                .login-header {
                    width: 100%;
                }
            }

            .login-wrap {
                padding: 24px;

                .el-input {
                    height: 38px;

                    input {
                        height: 38px;
                    }
                }
            }

            .input-icon {
                height: 39px;
                width: 14px;
                margin-left: 2px;
            }
        }
    }
}

@media (max-width: 1200px) {
    .login-form {
        width: 340px;
    }
}

@media (max-width: 1000px) {
    .login_main {
        display: block;
    }

    .login-form {
        width: 100%;
    }

    .login_adv {
        display: none;
    }
}

.login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
}

.login-code {
    width: 103px;
    height: 38px;
    float: right;

    img {
        cursor: pointer;
        vertical-align: middle;
    }
}

.el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
}

.login-code-img {
    height: 38px;
}
</style>